package com.clouddot.baipai.ui.Dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wxy on 2018/4/2.
 */

public class H_ContentDailog {
    private Dialog Dialog;
    private ConstraintLayout constraintLayout;
    private Context context;
    private TextView title, time, content;
    private List<TextView> textViews = new ArrayList<TextView>();
    private ImageView close;
    private View view;

    public H_ContentDailog(Context context) {
        this.context = context;
        initView();

    }

    public ImageView getCloseView() {
        if (close == null) {
            close = constraintLayout.findViewById(R.id.close);
        }
        return close;
    }

    @SuppressLint("WrongViewCast")
    public List<TextView> getTextViews() {
        if (textViews.isEmpty()) {
            title = constraintLayout.findViewById(R.id.title);
            time = constraintLayout.findViewById(R.id.time);
            content = constraintLayout.findViewById(R.id.content);
            textViews.add(title);
            textViews.add(time);
            textViews.add(content);
        }
        return textViews;
    }

    private void initView() {
        if (Env.getAppContext().banPaiType == 0) {
            constraintLayout = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.h_dialog_contentdialog, null);
        } else {
            constraintLayout = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.v_dialog_contentdialog, null);
        }
    }

    public void showDialog() {

        Dialog = getDialog();
        Dialog.show();

    }

    public void closeDialog() {
        if (Dialog != null) {
            Dialog.dismiss();
        }
    }

    public Dialog getDialog() {
        if (Dialog == null) {
            Dialog = new Dialog(context, R.style.ActionDialogStyle);
        }
        Dialog.setContentView(constraintLayout);
        Window dialogWindow = Dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        Dialog.setCancelable(false);
        /**
         *
         * 锁入地下的虚拟按键
         */
        view = dialogWindow.getDecorView();
        lock();
        view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int i) {
                lock();
            }
        });
        int uiFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        view.setSystemUiVisibility(uiFlags);

        return Dialog;
    }

    private void lock() {
        int uiFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        view.setSystemUiVisibility(uiFlags);
    }


}



